// olorSkin.h : Declaration of the ColorSkin

#ifndef __OLORSKIN_H_
#define __OLORSKIN_H_

#include "resource.h"       // main symbols
#include <atlctl.h>
#include<Atlbase.h>
#include <afxtempl.h>
#include "SkinWnd/SkinButton.h"
#include "SkinWnd/Subclass.h"
#include "SkinWnd/SkinWin.h"
#include "SkinWnd/MenuBarxp.h"
#include "../include/comhdr.h"

// {BE65B70F-F7B1-42ca-9F3F-D95F2ECEC599}
static const GUID CATID_PROJECT_FRAMEWORK_ADDINS = 
{ 0xbe65b70f, 0xf7b1, 0x42ca, { 0x9f, 0x3f, 0xd9, 0x5f, 0x2e, 0xce, 0xc5, 0x99 } };

#import "..\AddinManager\Debug\AddinManager.tlb" raw_interfaces_only, raw_native_types, no_namespace, named_guids 
/////////////////////////////////////////////////////////////////////////////
// ColorSkin
class ATL_NO_VTABLE ColorSkin : 
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<ColorSkin, &CLSID_olorSkin>,
	public ISupportErrorInfo,
	public IConnectionPointContainerImpl<ColorSkin>,
	public IDispatchImpl<IolorSkin, &IID_IolorSkin, &LIBID_COLORSKINADDINLib>,
	public IDispatchImpl<IFrameworkAddin, &IID_IFrameworkAddin, &LIBID_COLORSKINADDINLib>
{
		IFrameWork* m_pProjectFramework;
public:
	ColorSkin()
	{
		CString szModuleName,szModulePath;
		GetModuleFileName(AfxGetInstanceHandle(),szModuleName.GetBufferSetLength(MAX_PATH),MAX_PATH);
		szModuleName.ReleaseBuffer();
		int index = szModuleName.ReverseFind('\\');
		szModulePath = szModuleName.Left(index);
		
		m_szSkinXP = szModulePath + "\\skin\\xpstyle\\theme.ini";
		m_szSkinNeo= szModulePath + "\\skin\\neostyle\\theme.ini";
	}

DECLARE_REGISTRY_RESOURCEID(IDR_OLORSKIN)

DECLARE_PROTECT_FINAL_CONSTRUCT()

BEGIN_COM_MAP(ColorSkin)
	COM_INTERFACE_ENTRY(IolorSkin)
	COM_INTERFACE_ENTRY2(IDispatch,IolorSkin)
	COM_INTERFACE_ENTRY(ISupportErrorInfo)
	COM_INTERFACE_ENTRY(IConnectionPointContainer)
	COM_INTERFACE_ENTRY(IFrameworkAddin)
END_COM_MAP()
BEGIN_CONNECTION_POINT_MAP(ColorSkin)
END_CONNECTION_POINT_MAP()

BEGIN_CATEGORY_MAP(ColorMenu)
IMPLEMENTED_CATEGORY(CATID_PROJECT_FRAMEWORK_ADDINS)
END_CATEGORY_MAP()

// ISupportsErrorInfo
	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);

	static CString m_szSkinXP;
	static CString m_szSkinNeo;
	

// IolorSkin
public:
	STDMETHOD(OnSkinNeostyle)();
	STDMETHOD(OnSkinXP)();
	STDMETHOD(Initialize)(LONG lSessionID, IFrameWork * pApp, VARIANT bFirstTime)
	{
		AFX_MANAGE_STATE(AfxGetStaticModuleState())
		CString szModuleName,szModulePath;
		GetModuleFileName(AfxGetInstanceHandle(),szModuleName.GetBufferSetLength(MAX_PATH),MAX_PATH);
		szModuleName.ReleaseBuffer();
		int index = szModuleName.ReverseFind('\\');
		szModulePath = szModuleName.Left(index);

		_bstr_t message = L"Loading: " + szModuleName;
		pApp->LoggingService(message);

		CString XMLFileName;
		XMLFileName = szModuleName.Right(szModuleName.GetLength() - index - 1);
		XMLFileName = XMLFileName.Left(XMLFileName.ReverseFind('.'));
		XMLFileName = XMLFileName +  ".xml";

		CString szPath = szModulePath + "\\XML\\" + XMLFileName;
		
		CFile xmlfile;
		CFileException ex;
		if(!xmlfile.Open(szPath,CFile::modeRead,&ex))
		{
			TCHAR szError[1024];
			ex.GetErrorMessage(szError, 1024);
			NoticeBox(szError);
			return S_FALSE;
		}
		CString strXMLCommandSubFile;
		xmlfile.Read(strXMLCommandSubFile.GetBufferSetLength(xmlfile.GetLength()),xmlfile.GetLength());	
		strXMLCommandSubFile.ReleaseBuffer();
		
		strXMLCommandSubFile.Replace("\n",""); //Replace all new line characters
		strXMLCommandSubFile.Replace("\t",""); //Replace all tab characters
		
		HINSTANCE lAppHandle=AfxGetInstanceHandle();
		pApp->SetAddInInfo(lSessionID,(long)lAppHandle,strXMLCommandSubFile.AllocSysString(),IDR_TOOLBAR_ADDIN3); 
		m_pProjectFramework=pApp;
		return S_OK;
		
	}
	HRESULT _stdcall Uninitialize(LONG lSessionID);
	HRESULT _stdcall EndInitialize();
	static BOOL CALLBACK EnumSubingWndProc(HWND hwnd,LPARAM lParam);
	static LRESULT CALLBACK HookWndProc(int nCode, WPARAM wParam, LPARAM lParam);
	static BOOL CALLBACK EnumChildProc( HWND hwnd,LPARAM lParam);
	
	static CList<CSubclassWnd*,CSubclassWnd*> m_SubWndList;
	static CSkinButtonResource		m_btnres;		//skin button resource
	static CObList m_hookdlgbtns;

	static CMenuBarXP	m_wndMenuBar;
	static CReBarCtrl	m_wndReBar;

private:
	BOOL UpdateSkin(CString szResPath);
};

#endif //__OLORSKIN_H_
